<?
$queryAdd='';
if ($_SESSION['u_rule']!=0)
{
	$queryAdd=" AND root.mod_root_blocks_active='1' ";
}
$query=mysql_query ("
SELECT 
	summary_table.*, 
	langs_root.mod_lang_value, 
	langs_blocks.mod_lang_value
AS 
	blocks_langs_value,
	metka.mod_metka_value
FROM 
	(SELECT 
		root.mod_blocks_root_id, 
		root.mod_root_blocks_vposition,  
		root.mod_root_blocks_title, 
		root.mod_root_blocks_mkey,
		root.mod_root_blocks_type, 
		root.mod_root_blocks_active,
		blocks.mod_blocks_id,
		blocks.mod_blocks_root,
		blocks.mod_blocks_inroot_position, 
		blocks.mod_blocks_content, 
		blocks.mod_blocks_type, 
		blocks.mod_blocks_admin_target,
		blocks.mod_blocks_mkey, 
		blocks.mod_blocks_pkey,
		blocks.mod_blocks_active 
	FROM 
		".$lavpref."blocks_root root 
	JOIN 
		".$lavpref."blocks blocks 
	ON 
		root.mod_root_blocks_hposition='right'
	AND
		root.mod_root_blocks_right>='".$_SESSION['u_rule']."'
	AND
		blocks.mod_blocks_user_right>='".$_SESSION['u_rule']."' 
	AND
		root.mod_blocks_root_id=blocks.mod_blocks_root
	".$queryAdd." 
	) summary_table 
JOIN 
	".$lavpref."langs langs_root 
ON 
	summary_table.mod_root_blocks_title=langs_root.mod_lang_param 
AND 
	summary_table.mod_root_blocks_mkey=langs_root.mod_lang_module 
AND 
	langs_root.mod_lang_code='".$_SESSION['user_lang']."' 
JOIN
	".$lavpref."langs langs_blocks 
ON 
	summary_table.mod_blocks_content=langs_blocks.mod_lang_param 
AND 
	summary_table.mod_blocks_mkey=langs_blocks.mod_lang_module 
AND
	langs_blocks.mod_lang_code='".$_SESSION['user_lang']."'
LEFT JOIN
	".$lavpref."metka metka
ON
	summary_table.mod_blocks_content=metka.mod_metka_name
ORDER BY
	summary_table.mod_root_blocks_vposition, 
	summary_table.mod_blocks_inroot_position
ASC
");


$i=''; $t=0; 
while($result=mysql_fetch_array($query))
{
	if ($result['mod_blocks_root_id']!=$i)
	{
		$tt=0; $f=$t;
		$rootId[$t]=$result['mod_blocks_root_id'];
		$rootLangValue[$t]=$result['mod_lang_value'];
		$rootActive[$t]=$result['mod_root_blocks_active'];
		$rootType[$t]=$result['mod_root_blocks_type'];
		$blocksValueArray['id'][$t][$tt]=$result['mod_blocks_id'];
		$blocksValueArray['name'][$t][$tt]=$result['blocks_langs_value'];
		$blocksValueArray['module'][$t][$tt]=$result['mod_blocks_mkey'];
		$blocksValueArray['page'][$t][$tt]=$result['mod_blocks_pkey'];
		$blocksValueArray['type'][$t][$tt]=$result['mod_blocks_type'];
		$blocksValueArray['target'][$t][$tt]=$result['mod_blocks_admin_target'];
		$blocksValueArray['activated'][$t][$tt]=$result['mod_blocks_active'];
		$blocksValueArray['metka'][$t][$tt]=$result['mod_metka_value'];
		
		$t++;
		$i=$result['mod_blocks_root_id'];
	}else
	{
		$tt++;
		$blocksValueArray['id'][$f][$tt]=$result['mod_blocks_id'];
		$blocksValueArray['name'][$f][$tt]=$result['blocks_langs_value'];
		$blocksValueArray['module'][$f][$tt]=$result['mod_blocks_mkey'];
		$blocksValueArray['page'][$f][$tt]=$result['mod_blocks_pkey'];
		$blocksValueArray['type'][$f][$tt]=$result['mod_blocks_type'];
		$blocksValueArray['target'][$f][$tt]=$result['mod_blocks_admin_target'];
		$blocksValueArray['activated'][$f][$tt]=$result['mod_blocks_active'];
		$blocksValueArray['metka'][$f][$tt]=$result['mod_metka_value'];
	}
}


for($i=0; $i<count($rootLangValue); $i++)
{
	$rootTableClass=$rootType[$i];
	if($rootActive[$i]==0)
		$rootTableClass=$rootType[$i].'_nonactive';
	
	$blocksTdClass='td_'.$rootType[$i];
	
	print'<table border="0" align="center" cellpadding="1" cellspacing="0" width="100%" class="'.$rootTableClass.'">
		<tr>
			<td align="center" valign="top"><b>'.$rootLangValue[$i].'</b></td>
		</tr>
		<tr>
			<td>
				<table border="1" width="100%" cellpadding="1" cellspacing="0" class="blocksLeft_'.$rootType[$i].'">';
					for ($t=0; $t<count($blocksValueArray['id'][$i]); $t++)
					{
						if ($blocksValueArray['activated'][$i][$t]==0)
							$blocksTdClass='td_'.$rootType[$i].'_nonactive';
						
						print '<tr>';
						if ($blocksValueArray['type'][$i][$t]=='metka')
						{
							print '<td class="'.$blocksTdClass.'">';
								eval($blocksValueArray['metka'][$i][$t]);
							print'</td>';
						}
						elseif($blocksValueArray['type'][$i][$t]=='link')
						{
							print '<td class="'.$blocksTdClass.'">';
							CreateLink($_SESSION['option']['portal_url'], $blocksValueArray['target'][$i][$t], $blocksValueArray['module'][$i][$t], $blocksValueArray['page'][$i][$t], $blocksValueArray['name'][$i][$t], '', 'print', array('','link_'.$rootType[$i],'link_'.$rootId[$i].'_'.$blocksValueArray['id'][$i][$t],'',''));
							print'</td>';
						}
						print '</tr>';
					}
				print '</table>
			</td>
		</tr>
	</table>';
}
unset ($rootId);
unset ($rootLangValue);
unset ($rootActive);
unset ($rootType);
unset ($blocksValueArray);
?>